package com.controller.aop;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.common.model.ActionEnum;
import com.dao.sys.ExternalLogDao;
import com.dao.sys.LogDao;
import com.model.supply.SupplyUserModel;
import com.model.sys.ExternalLogModel;
import com.model.sys.LogModel;
import com.service.aop.ServiceAspect;

@Component
@Aspect
public class ControllerAspect {

	@Autowired
	ExternalLogDao externalLogDao;

	// 定义一个全局的记录器，通过LoggerFactory获取
	private final static Logger logger = LoggerFactory.getLogger(ServiceAspect.class);

	@Around(value = "execution(* com.controller..*.*(..))")
	public Object controllerAround(ProceedingJoinPoint point) throws Throwable {
		long begin = System.currentTimeMillis();
		Object result = point.proceed(); // 调用目标方法
		logger.info("controllerAround测试调用类：{},调用方法:{},调用时间：{}", point.getSignature().getDeclaringTypeName(),
				point.getSignature().getName(), System.currentTimeMillis() - begin);
		return result;
	}

	@Pointcut("execution(* com.controller..*.*(..))")
	public void cutService() {

	}

	/**
	 * 记录外部接口请求日志保存数据库
	 * 
	 * @param point
	 * @return
	 * @throws Throwable
	 */
	@Around(value = "execution(* com.controller.external..*.*(..))")
	public Object externalControllerAround(ProceedingJoinPoint point) throws Throwable {
		Object[] objects = point.getArgs();
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (attributes == null) {
			return point.proceed();
		}
		HttpServletRequest request = attributes.getRequest();
		SupplyUserModel sysUser = (SupplyUserModel) request.getSession().getAttribute("supplyUserModel");
		if (sysUser == null) {
			return point.proceed();
		}
		long begin = System.currentTimeMillis();
		Object result = point.proceed(); // 调用目标方法
		if (objects != null && objects.length > 0) {
			for (Object arg : objects) {
				logger.debug("externalControllerAround-receive:{}", arg.toString());
				ExternalLogModel externalLogModel = new ExternalLogModel();
				externalLogModel.setExtId(UUID.randomUUID().toString());
				externalLogModel.setExtClass(point.getSignature().getDeclaringTypeName());
				externalLogModel.setExtMethod(point.getSignature().getName());
				externalLogModel.setExtTime(System.currentTimeMillis() - begin);
				externalLogModel.setExtSendBody(JSON.toJSONString(result));
				externalLogModel.setExtReceiveBody(JSON.toJSONString(arg));
				externalLogModel.setRemark("1");// 1代表外部调用，0代表调用外部
				externalLogModel.setCreateId(sysUser.getUserId());
				externalLogModel.setCreateName(sysUser.getSupplyName());
				externalLogModel.setUpdateName(sysUser.getSupplyName());
				externalLogDao.insert(externalLogModel);
				logger.debug("externalControllerAround-send:{}", JSON.toJSONString(result));
			}
		}
		return result;
	}
}
